package com.gaoqi.mq.router;

import com.gaoqi.mq.utils.RabbitMQ;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.testng.annotations.Test;

/**
 * 该类用于模拟 rabbitmq 路由模式 中的生产者
 */
@Slf4j
public class Provider {

    @SneakyThrows
    @Test
    public void routerSendMessage() {
        //获取mq 连接对象
        Connection rabbitConnection = RabbitMQ.getRabbitConnection();
        //获取通道对象
        Channel channel = rabbitConnection.createChannel();

        //通道配置交换机名及交换机类型
        channel.exchangeDeclare("logs_router", "direct");

        //指定route key
        String routeKey = "warning";

        //发布消息 并指定相应route key
        channel.basicPublish("logs_router", routeKey, null, (routeKey + " This is route message!!!").getBytes());

        //关闭连接及 通道
        RabbitMQ.closeChannelAndConnection(channel, rabbitConnection);
    }
}
